package cn.lfy.studentmanager.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.lfy.studentmanager.domain.Student;
import cn.lfy.studentmanager.service.StudentService;


@Controller
@RequestMapping("/student")
public class StudentController {
	//要使用数据库，先注入service（service区调用dao，dao操作数据库）
	@Autowired
	private StudentService studentService;

	//controller调用service（这是接口），说是由service调用dao接口，其实是在service实现类中调用dao接口,都是通过注解注入来调用的
	//录入学生信息

	@PostMapping("add")
	//引入实体类
	public ModelAndView add(Student student, ModelMap modelMap){
		//接收学生信息，保存到数据库
		//try catch可以查到异常，在这里，只要有异常就会输出失败（不过现在更改过了，不会出现在页面了）
		try {
			//下面的student就是上面引入的实体类，通过类来调用方法，这里设置这几项属性的内容
			student.setCid(0);
			student.setSid(0);
			student.setPasswd(Integer.valueOf(student.getNumber()).toString());
			if(student.getName()!=null&&student.getName()!="") {
				//判断当前输入无误对下面的操作在进行其他判断
				
				//valueOf方法是将int型转换成Integer型，toString()方法是将Integer型转换成String型
				//输出在控制台
				System.out.println(student.getName());
				System.out.println(student.getNumber());
				System.out.println(student.getSex());
				
				//if（）括号里就是true或false，这里是用来判断输入
				//必须与数据库中的一致
				boolean isServiceSuccess = studentService.add(student);
				if(isServiceSuccess) {
					modelMap.addAttribute("msg", "成功");
				}else {
					modelMap.addAttribute("msg", "失败");
				}
				
			}else {
				modelMap.addAttribute("msg", "失败");
				return new ModelAndView("/student/add_index",modelMap);
					
			}
			
		} catch (Exception e) {
			modelMap.addAttribute("msg", "失败");
		}
		
//		return "student/select_index";
		return new ModelAndView("redirect:/student/select_index", modelMap);
		//从add_index页面直接跳转到select_index页面，并且会完成查询功能
		//访问add_index，放回给一个地址（）
	}
	
	
	
	@GetMapping("add_index")
	public String add_index(ModelMap modelMap){
		return "student/add_index";
		//return new ModelAndView("student/add_index");
	}
	//http://localhost:8080/student/add_index
	
	//查询信息
	@GetMapping("select_index")
	public String select(HttpServletRequest request, ModelMap modelMap){
		//String msg=request.getParameter("msg");
		//这里使用的返回值是String
		//从数据库查询到所有学生数据
		//把查询到的学生数据塞到html
//		List<Student> list=studentService.select();
//		modelMap.addAttribute("students", list);
		modelMap.addAttribute("students", studentService.select());
		modelMap.addAttribute("error", request.getParameter("msg"));
		//modelMap.addAttribute("msg",msg);
		return "student/select_index";
	}

	//查询信息

	//删除信息
	@GetMapping("delete")
	public ModelAndView delete(@RequestParam( required = true)int id , ModelMap modelMap) {
	//给出要删除的参数  @RequestParam( required = true)int id 
	System.out.println(id);
	studentService.delete(id);
	
	return new ModelAndView("redirect:/student/select_index", modelMap);
	}
	//进入更新信息页面
	@GetMapping("update_index")
	public String update(@RequestParam( required = true)int id,ModelMap modelMap) {
		//查询数据库学生信息,查谁？根据id查询  @RequestParam( required = true)int id 
		modelMap.addAttribute("student", studentService.query(id));
		
		return "student/update_index";
	}
	
	
	//更新完成，跳转，到select_index页面
	@PostMapping("update")
	public ModelAndView updatesuccess(Student student, ModelMap modelMap){
		studentService.update(student);
		//调用service中的
		
		return new ModelAndView("redirect:/student/select_index", modelMap);
	}
	@GetMapping("")
//	将url设置成student/
	public ModelAndView index(ModelMap modelMap) {
	
	return new ModelAndView("redirect:/student/select_index", modelMap);
	}
	
	
//	@GetMapping("test")
//	public String hehe(ModelMap modelMap) {
//		return "Test.html";
//		
//	}
	
}